import { storage, href } from "cl-admin/utils";
// import chatDB from "@/cool/modules/chat/utils/db";

// 用户信息
let info = storage.get("userInfo") || {};
// 授权标识
let token = storage.get("token") || null;

export default {
	state: {
		token,
		info
	},
	getters: {
		userInfo: state => state.info,
		token: state => state.token,
		rules: state => {
			const rules = state.info?.rule;
			if (rules == "*") return "*";
			return rules?.map?.(v => v.rule_sign);
		}
	},
	actions: {
		// 用户登录
		selectUserLogin({ commit, dispatch }, form) {
			return this.$service.open.selectUserLogin(form).then(async res => {
				commit("SET_TOKEN", res);
				commit("SET_USERINFO", res);
				// 权限菜单;
				await Promise.all([dispatch("permMenu"), dispatch("biz/getBusiness")]);
				return res;
			});
		},

		// 用户登录
		ddLogin({ commit, dispatch }, form) {
			return this.$service.open.ddLogin(form).then(async res => {
				commit("SET_TOKEN", res);
				commit("SET_USERINFO", res);
				// 权限菜单;
				await Promise.all([dispatch("permMenu"), dispatch("biz/getBusiness")]);
				return res;
			});
		},
		// 登录
		userLogin({ commit, dispatch }, form) {
			return this.$service.open.userLogin(form).then(async res => {
				commit("SET_TOKEN", res);
				commit("SET_USERINFO", res);
				// 权限菜单;
				await Promise.all([dispatch("permMenu"), dispatch("biz/getBusiness")]);
				return res;
			});
		},

		// 用户退出
		async userLogout({ dispatch }) {
			await dispatch("userRemove");
			await dispatch("common/delAllViews");
		},
		// 重置密码
		userResetPwd({ commit }, form) {
			return this.$service.user.resetpwd(form).then(res => {
				commit("SET_TOKEN", res);
				commit("SET_USERINFO", res);
				return res;
			});
		},
		// 用户信息
		userInfo({ commit }, data) {
			// return this.$service.common.userInfo().then(res => {
			// 	commit("SET_USERINFO", res);
			// 	return res;
			// });
			commit("SET_USERINFO", data);
		},

		// 用户移除
		async userRemove({ commit }) {
			commit("CLEAR_USER");
			commit("CLEAR_TOKEN");
			commit("RESET_PROCESS");
			commit("SET_MENU_GROUP", []);
			commit("SET_VIEW_ROUTES", []);
			commit("SET_MENU_LIST", 0);
			commit("CLEAR_SESSION_LIST");
			commit("common/SET_BUSINESS", []);
			commit("biz/ALL_RESET");
			// await chatDB.clearTable().catch(() => true);
		},

		// 刷新token
		async refreshToken({ commit, dispatch }) {
			await this.$service.open.refreshToken(storage.get("refreshToken")).catch(err => {
				dispatch("userRemove");
				href("/login");
				reject(err);
			});
			commit("SET_TOKEN", res);
			return res.token;
		}
	},
	mutations: {
		// 设置用户信息
		SET_USERINFO(state, val) {
			state.info = val;
			storage.set("userInfo", val);
		},

		// 设置授权标识
		SET_TOKEN(state, { token, token_expire, refreshToken, refreshExpire }) {
			// 请求的唯一标识
			state.token = token;
			storage.set("token", token, token_expire);

			// 刷新 token 的唯一标识
			storage.set("refreshToken", refreshToken, refreshExpire);
		},

		// 移除授权标识
		CLEAR_TOKEN(state) {
			state.token = null;
			storage.remove("token");
			storage.remove("refreshToken");
		},

		// 移除用户信息
		CLEAR_USER(state) {
			state.info = {};
			storage.remove("userInfo");
		}
	}
};
